package com.mingsoft.cms.action.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.mingsoft.base.filter.DateValueFilter;
import com.mingsoft.base.filter.DoubleValueFilter;
import com.mingsoft.basic.action.BaseAction;
import com.mingsoft.basic.entity.ColumnEntity;
import com.mingsoft.cms.biz.IBasePropertyBiz;
import com.mingsoft.cms.biz.IPropertyValueBiz;
import com.mingsoft.cms.constant.ModelCode;
import com.mingsoft.cms.entity.BasePropertyEntity;
import com.mingsoft.cms.entity.PropertyValueEntity;
import com.mingsoft.util.StringUtil;

import net.mingsoft.base.util.JSONObject;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.util.BasicUtil;

@Controller("propertyValueAction")
@RequestMapping("/${managerPath}/goods/propertyValue")
public class PropertyValueAction extends BaseAction{
    /**
     * 注入博客文章业务层
     */    
    @Resource(name="propertyValueImpl")
    private IPropertyValueBiz propertyValueBiz;
    /**
     * 注入博客文章业务层
     */    
    @Resource(name="basePropertyImpl")
    private IBasePropertyBiz basePropertyBiz;
    /**
     * 返回主界面index
     */
    @RequestMapping("/index")
    public String index(HttpServletResponse response,HttpServletRequest request){
    	// 获取站点id
		List<BasePropertyEntity> list = basePropertyBiz.queryAll();
		request.setAttribute("listColumn", JSONArray.toJSONString(list));
        return view ("/goods/pro_value_index");
    }
    /**
     * 返回主界面index
     */
    @RequestMapping("/main")
    public String main(HttpServletResponse response,HttpServletRequest request,ModelMap mode){
    	// 获取站点id
        String bpId = request.getParameter("propertyId");
        if(StringUtils.isBlank(bpId)){
        	bpId = "";
        }
    	mode.addAttribute("bpId", bpId);
    	return view ("/goods/pro_value_main");
    }
    /**
     * 查询博客文章列表
     * @param basePropery 暂无描述实体
     * <i>baseProperty参数包含字段信息参考：</i><br/>
     * basePropertyBasicId 博客文章自增长ID<br/>
     * basePropertyContent 文章内容<br/>
     * <dt><span class="strong">返回</span></dt><br/>
     * <dd>[<br/>
     * { <br/>
     * basePropertyBasicId: 博客文章自增长ID<br/>
     * basePropertyContent: 文章内容<br/>
     * }<br/>
     * ]</dd><br/>     
     */
    @RequestMapping("/list")
    @ResponseBody
    public void list(@ModelAttribute PropertyValueEntity propertyValue,HttpServletResponse response, HttpServletRequest request,ModelMap model) {
    
    	Integer bpId = propertyValue.getPropertyId();
        BasicUtil.startPage();
//        List basePropertyList = propertyValueBiz.queryAll();
        List basePropertyList = propertyValueBiz.query(propertyValue);
//        List basePropertyList = new ArrayList();
        this.outJson(response, net.mingsoft.base.util.JSONArray.toJSONString(new EUListBean(basePropertyList,(int)BasicUtil.endPage(basePropertyList).getTotal()),new DoubleValueFilter(),new DateValueFilter()));
    }

    /**
     * 返回编辑界面baseProperty_form
     */
    @RequestMapping("/form")
    public String form(@ModelAttribute PropertyValueEntity propertyValue,HttpServletResponse response,HttpServletRequest request,ModelMap model){
        if(propertyValue.getPvId() != null){
        	PropertyValueEntity propertyValueEntity = (PropertyValueEntity) propertyValueBiz.getEntity(propertyValue.getPvId());            
            model.addAttribute("propertyValue",propertyValueEntity);
        }else{
        	model.addAttribute("propertyValue",new PropertyValueEntity());
        }
        
        List basePropertyList = basePropertyBiz.queryAll();
        model.addAttribute("basePropertyList",basePropertyList);
        model.addAttribute("bpId",propertyValue.getPropertyId());
        return view ("/goods/pro_value_from");
    }

    /**
     * 获取博客文章
     * @param propertyValue 博客文章实体
     * <i>baseProperty参数包含字段信息参考：</i><br/>
     * basePropertyBasicId 博客文章自增长ID<br/>
     * basePropertyContent 文章内容<br/>
     * <dt><span class="strong">返回</span></dt><br/>
     * <dd>{ <br/>
     * basePropertyBasicId: 博客文章自增长ID<br/>
     * basePropertyContent: 文章内容<br/>
     * }</dd><br/>
     */
    @RequestMapping("/get")
    @ResponseBody
    public void get(@ModelAttribute PropertyValueEntity propertyValue,HttpServletResponse response, HttpServletRequest request,ModelMap model){
        if( 0 == propertyValue.getPvId()  ) {
            this.outJson(response, null, false, getResString("err.error", this.getResString("baseProperty.pvId")));
            return;
        }
        PropertyValueEntity _propertyValue = (PropertyValueEntity)propertyValueBiz.getEntity(propertyValue);
        this.outJson(response, _propertyValue);
    }

    /**
     * 保存博客文章实体
     * @param baseProperty 博客文章实体
     * <i>baseProperty参数包含字段信息参考：</i><br/>
     * basePropertyBasicId 博客文章自增长ID<br/>
     * basePropertyContent 文章内容<br/>
     * <dt><span class="strong">返回</span></dt><br/>
     * <dd>{ <br/>
     * basePropertyBasicId: 博客文章自增长ID<br/>
     * basePropertyContent: 文章内容<br/>
     * }</dd><br/>
     */
    @PostMapping("/save")
    @RequiresPermissions("baseProperty:save")
    @ResponseBody
    public void save(@ModelAttribute PropertyValueEntity propertyValue, HttpServletResponse response, HttpServletRequest request) {
        //验证文章内容的值是否合法            
        if(StringUtil.isBlank(propertyValue.getPropertyValue())){
            this.outJson(response, null,false,getResString("err.empty", this.getResString("baseProperty.propertyValue")));
            return;            
        }
        propertyValueBiz.saveEntity(propertyValue);//如果是普通模版 直接调用saveEntity(baseProperty)

        this.outJson(response, JSONObject.toJSONString(propertyValue));
    }

    /**
     * @param baseProperty 博客文章实体
     * <i>baseProperty参数包含字段信息参考：</i><br/>
     * basePropertyBasicId:多个basePropertyBasicId直接用逗号隔开,例如basePropertyBasicId=1,2,3,4
     * 批量删除暂无描述
     *            <dt><span class="strong">返回</span></dt><br/>
     *            <dd>{code:"错误编码",<br/>
     *            result:"true｜false",<br/>
     *            resultMsg:"错误信息"<br/>
     *            }</dd>
     */
    @RequestMapping("/delete")
    @RequiresPermissions("baseProperty:delete")
    @ResponseBody
    public void delete(@RequestBody List<PropertyValueEntity> propertyValues,HttpServletResponse response, HttpServletRequest request) {
        int[] ids = new int[propertyValues.size()];
        for(int i = 0;i<propertyValues.size();i++){
            ids[i] = propertyValues.get(i).getPvId();
        }
        propertyValueBiz.deleteBasic(ids);    
        this.outJson(response, true);
    }

    /** 
     * 更新博客文章
     * @param baseProperty 博客文章实体
     * <i>baseProperty参数包含字段信息参考：</i><br/>
     * basePropertyBasicId 博客文章自增长ID<br/>
     * basePropertyContent 文章内容<br/>
     * <dt><span class="strong">返回</span></dt><br/>
     * <dd>{ <br/>
     * basePropertyBasicId: 博客文章自增长ID<br/>
     * basePropertyContent: 文章内容<br/>
     * }</dd><br/>
     */
    @PostMapping("/update")
    @ResponseBody     
    public void update(@ModelAttribute PropertyValueEntity propertyValue, HttpServletResponse response,
            HttpServletRequest request) {
        //验证文章内容的值是否合法            
        if(StringUtil.isBlank(propertyValue.getPropertyValue())){
            this.outJson(response, null,false,getResString("err.empty", this.getResString("baseProperty.propertyValue")));
            return;            
        }
        propertyValueBiz.updateEntity(propertyValue);//如果是普通模版 直接调用updateEntity(baseProperty)
        this.outJson(response, JSONObject.toJSONString(propertyValue));
    }

}